package com.andy.blog.dal.service.impl;

import com.andy.blog.common.constant.UserConstants;
import com.andy.blog.common.exception.BlogServiceException;
import com.andy.blog.common.reponse.BlogResultCode;
import com.andy.blog.common.utils.DateUtils;
import com.andy.blog.common.utils.SecurityUtils;
import com.andy.blog.common.utils.TransformUtils;
import com.andy.blog.dal.domain.SysConfigDO;
import com.andy.blog.dal.mapper.SysConfigMapper;
import com.andy.blog.dal.service.SysConfigDalService;
import com.andy.blog.dal.vo.SysConfigVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 参数配置
 * Created by andy on 2023-04-05 11:38
 */
@Service
public class SysConfigDalServiceImpl implements SysConfigDalService {

    @Resource
    private SysConfigMapper configMapper;


    @Override
    public SysConfigDO selectOneConfig(SysConfigVO sysConfigVO) {
        LambdaQueryWrapper<SysConfigDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper
                .eq(StringUtils.isNotBlank(sysConfigVO.getConfigKey()), SysConfigDO::getConfigKey, sysConfigVO.getConfigKey())
                .eq(Objects.nonNull(sysConfigVO.getId()), SysConfigDO::getId, sysConfigVO.getId());

        return configMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public int insertConfig(SysConfigVO configVO) {
        SysConfigDO sysConfigDO = TransformUtils.transform(configVO, SysConfigDO.class);

        sysConfigDO.setCreateBy(SecurityUtils.getUsername());
        sysConfigDO.setCreateTime(DateUtils.getCurrentDate());

        try {
            return configMapper.insert(sysConfigDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100, e);
        }
    }


    @Override
    public int updateConfig(SysConfigVO configVO) {
        SysConfigDO sysConfigDO = TransformUtils.transform(configVO, SysConfigDO.class);

        sysConfigDO.setUpdateBy(SecurityUtils.getUsername());
        sysConfigDO.setUpdateTime(DateUtils.getCurrentDate());

        try {
            return configMapper.updateById(sysConfigDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000101, e);
        }
    }


    @Override
    public boolean checkConfigKeyUnique(SysConfigVO configVO) {
        LambdaQueryWrapper<SysConfigDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysConfigDO::getConfigKey, configVO.getConfigKey()).last("limit 1");
        SysConfigDO sysConfigDO = configMapper.selectOne(lambdaQueryWrapper);

        if (Objects.nonNull(sysConfigDO) && !Objects.equals(sysConfigDO.getId(), configVO.getId())) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 构建查询条件
     */
    private LambdaQueryWrapper<SysConfigDO> buildQueryWrapper(SysConfigVO sysConfigVO) {
        LambdaQueryWrapper<SysConfigDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (Objects.isNull(sysConfigVO)) {
            return lambdaQueryWrapper;
        }
        lambdaQueryWrapper
                .like(StringUtils.isNotBlank(sysConfigVO.getConfigName()), SysConfigDO::getConfigName, sysConfigVO.getConfigName())
                .like(StringUtils.isNotBlank(sysConfigVO.getConfigKey()), SysConfigDO::getConfigKey, sysConfigVO.getConfigKey())
                .ge(StringUtils.isNotBlank(sysConfigVO.getBeginTime()), SysConfigDO::getCreateTime, sysConfigVO.getBeginTime())
                .le(StringUtils.isNotBlank(sysConfigVO.getEndTime()), SysConfigDO::getCreateTime, sysConfigVO.getEndTime())
                .orderByDesc(SysConfigDO::getId);
        return lambdaQueryWrapper;
    }

    @Override
    public IPage<SysConfigDO> selectListByPage(SysConfigVO sysConfigVO) {
        LambdaQueryWrapper<SysConfigDO> lambdaQueryWrapper = buildQueryWrapper(sysConfigVO);

        IPage<SysConfigDO> page = new Page<>(sysConfigVO.getPageNum(), sysConfigVO.getPageSize());

        return configMapper.selectPage(page, lambdaQueryWrapper);
    }

    @Override
    public List<SysConfigDO> selectListByCondition(SysConfigVO sysConfigVO) {
        LambdaQueryWrapper<SysConfigDO> lambdaQueryWrapper = buildQueryWrapper(sysConfigVO);

        return configMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public SysConfigDO selectById(Long id) {
        return configMapper.selectById(id);
    }

    @Override
    public int deleteById(Long id) {
        return configMapper.deleteById(id);
    }
}
